/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.Comparator;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * <p>Project: javase - Stream2
 * <p>Powered by webrx On 2021-12-22 15:28:32
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Stream2 {
    public static void main(String[] args) {
        //var i = new AtomicInteger();
        //Stream<Integer> si = Stream.generate(() -> i.incrementAndGet()).limit(5);
        //si.forEach(System.out::println);

        Random rand = new Random();
        Stream<Integer> ss = Stream.generate(() -> rand.nextInt(100) + 1).limit(10);
        //升序
        //ss.sorted().forEach(e -> System.out.printf("%02d ", e));

        //降序
        //ss.sorted((a, b) -> b - a).forEach(e -> System.out.printf("%02d ", e));
        //ss.sorted(Comparator.reverseOrder()).forEach(e -> System.out.printf("%02d ", e));

        //乱序
        ss = Stream.of(1, 5, 2, 35, 15, 9);
        ss.sorted((a, b) -> rand.nextBoolean() ? 1 : -1).forEach(e -> System.out.printf("%02d ", e));

        System.out.println();

        String str = "java\njavascript\nmysql\nhtml5\njava8\njava17";
        var sss = str.lines();
        //sss.sorted().forEach(System.out::println);
        //sss.filter(e -> e.startsWith("java")).sorted(Comparator.reverseOrder()).forEach(System.out::println);
        sss
                .filter(e -> e.startsWith("java") && e.length() > 5)
                .map(e -> String.format("hello:%s", e.toUpperCase()))
                .filter(e -> e.length() > 5)
                .sorted(Comparator.comparingInt(String::length))
                .forEach(System.out::println);

    }
}
